package com.coach.model;

import java.util.ArrayList;
import java.util.List;

public class Folder extends TreeModel {
	protected List<Folder> folders;
	
	public List<Folder> getFolders() {
		return folders;
	}

	public List<TrainingSet> getSets() {
		return sets;
	}
	
	public int size() {
		return folders.size() + sets.size();
	}

	protected List<TrainingSet> sets;
	
	private static IModelVisitor adder = new Adder();
	private static IModelVisitor remover = new Remover();
	
	public Folder() {
		folders = new ArrayList<Folder>();
		sets = new ArrayList<TrainingSet>();
	}

	@Override
	public void accept(IModelVisitor visitor, Object args) {
		visitor.visitFolder(this, args);
	}

	@Override
	public void add(Model model) {
		model.accept(adder, this);
	}

	@Override
	public void remove(Model model) {
		model.accept(remover, this);
	}
	
	public void addFolder(Folder newFolder) {
		folders.add(newFolder);
		newFolder.parent = this;
		fireAddEvent(newFolder);
	}
	
	public void addTrainingSet(TrainingSet newTrainingSet) {
		sets.add(newTrainingSet);
		newTrainingSet.parent = this;
		fireAddEvent(newTrainingSet);
	}
	
	public void removeFolder(Folder delFolder) {
		folders.remove(delFolder);
		delFolder.addChangeListener(ModelChangeListener.getInstance());
		fireRemoveEvent(delFolder);
	}
	
	public void removeTrainingSet(TrainingSet delTrainingSet) {
		sets.remove(delTrainingSet);
		delTrainingSet.addChangeListener(ModelChangeListener.getInstance());
		fireRemoveEvent(delTrainingSet);
	}

	private static class Adder implements IModelVisitor {

		@Override
		public void visitFolder(Folder folder, Object args) {
			((Folder)args).addFolder(folder);
		}

		@Override
		public void visitTrainingSet(TrainingSet trainingSet, Object args) {
			((Folder)args).addTrainingSet(trainingSet);
		}
		
	}
	
	private static class Remover implements IModelVisitor {

		@Override
		public void visitFolder(Folder folder, Object args) {
			((Folder)args).removeFolder(folder);
		}

		@Override
		public void visitTrainingSet(TrainingSet trainingSet, Object args) {
			((Folder)args).removeTrainingSet(trainingSet);
		}
		
	}
	
}
